/*
 * Copyright (c) 2011, Petr Panteleyev <petr@panteleyev.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.gclient;

import com.google.common.collect.ImmutableList;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.panteleyev.blogclient.BloggerEditorFrame;
import org.panteleyev.blogclient.IPicturePublishDialog;
import org.panteleyev.blogclient.PublishTargetPanel;
import org.panteleyev.blogclient.UserProfileManager;
import org.panteleyev.blogclient.photo.ThumbnailsPublishPanel;
import org.panteleyev.photo.Thumbnail;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SwingFactory;
import org.panteleyev.swing.SwingWorkerEx;

public class GAlbumPublishDialog extends BasicDialog<JFrame> implements IPicturePublishDialog {
    private ImmutableList<GAlbum> albums;

    private JCheckBox           useTableCheckBox;
    private JComboBox<String>   columnsComboBox;
    private JCheckBox           tableBorderCheckBox;
    private JCheckBox           imageBorderCheckBox;
    private JCheckBox           titleCheckBox;
    private JComboBox<String>   sizeComboBox;
    private PublishTargetPanel  targetPanel;
    private ThumbnailsPublishPanel<GAlbum> thumbnailsPanel;

    private GBUserProfile       profile;

    public GAlbumPublishDialog(JFrame parent, GBUserProfile profile, final ImmutableList<GAlbum> albums) {
        super(parent);
        this.profile = profile;
        this.albums = albums;

        setTitle(TITLE);
        setResizable(true);
        initComponents();

        new SwingWorkerEx<Object,Object>(this) {
            private ArrayList<Thumbnail<GAlbum>> thumbnails = new ArrayList<>();

            @Override
            protected Object doInBackground() throws Exception {
                for (final GAlbum album : albums) {
                    ImageIcon image = UserProfileManager.loadThumbnailFromNetwork(getProfile(), album.getThumbnailUrl());
                    thumbnails.add(new Thumbnail<>(image, album.getTitle(), album));
                }
                return null;
            }
            @Override
            protected void done() {
                thumbnailsPanel.setContent(thumbnails);
                super.done();
            }
        }.execute();
    }

    GBUserProfile getProfile() {
        return profile;
    }

    private void initComponents() {
        getContentPane().setLayout(new BorderLayout());
        JPanel tablePanel;

        final AbstractAction useTableAction = new AbstractAction(USE_TABLE) {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                final boolean b = useTableCheckBox.isSelected();
                columnsComboBox.setEnabled(b);
                tableBorderCheckBox.setEnabled(b);
                titleCheckBox.setEnabled(b);
            }
        };

        JPanel mainPanel = SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_Y,
            tablePanel = SwingFactory.createPanel(BorderFactory.createTitledBorder(TABLE), SwingFactory.LAYOUT.BOX_Y,
                useTableCheckBox = new JCheckBox(useTableAction),
                SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                    new JLabel(COLUMNS),
                    Box.createHorizontalStrut(3),
                    columnsComboBox = new JComboBox<>(new String[] { "1", "2", "3", "4" }),
                    Box.createHorizontalStrut(5),
                    tableBorderCheckBox = new JCheckBox(BORDER),
                    Box.createHorizontalStrut(5),
                    titleCheckBox = new JCheckBox(ITEM_TITLE),
                    Box.createHorizontalGlue()
                )
            ),
            SwingFactory.createPanel(
                BorderFactory.createTitledBorder(LINK), SwingFactory.LAYOUT.BOX_X,
                sizeComboBox = new JComboBox<>(),
                Box.createHorizontalStrut(5),
                imageBorderCheckBox = new JCheckBox(BORDER),
                Box.createHorizontalGlue()
            ),
            targetPanel = new PublishTargetPanel()
        );
        SwingFactory.setAlignmentX(tablePanel, LEFT_ALIGNMENT);
        SwingFactory.setAlignmentX(mainPanel, LEFT_ALIGNMENT);
        useTableCheckBox.setSelected(true);

        for (MediaThumbnail t : albums.get(0).getThumbnails()) {
            sizeComboBox.addItem(new StringBuilder().append(t.getWidth()).append("x").append(t.getHeight()).toString());
        }

        SwingFactory.setWidthMaximumToPreferred(columnsComboBox, sizeComboBox);

        JTabbedPane pane = new JTabbedPane();
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(mainPanel, BorderLayout.NORTH);
        pane.add(SETTINGS, panel);
        pane.add(ALBUMS, thumbnailsPanel = new ThumbnailsPublishPanel<>());

        getContentPane().add(pane, BorderLayout.CENTER);
        getContentPane().add(new OkCancelButtonPanel(this), BorderLayout.SOUTH);
    }

    public Thumbnail<GAlbum> getThumbnail(int index) {
        return thumbnailsPanel.getThumbnail(index);
    }

    public boolean getTableBorder() {
        return tableBorderCheckBox.isSelected();
    }

    public boolean getImageBorder() {
        return imageBorderCheckBox.isSelected();
    }

    public int getColumnCount() {
        return columnsComboBox.getSelectedIndex() + 1;
    }

    int getSmallSizeIndex() {
        return sizeComboBox.getSelectedIndex();
    }

    BloggerEditorFrame<?> getDestinationWindow() {
        return targetPanel.getDestinationWindow();
    }

    public boolean isAddTitle() {
        return titleCheckBox.isSelected();
    }

    public boolean isUseTable() {
        return useTableCheckBox.isSelected();
    }

}
